package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressBookCtr abCtr;//reference to the ControlLayer

    //constructor which does nothing
    public AddressBookUI()
    {
        abCtr = abCtr.getInstance();
    }

    public void start()
    {
        addressMenu();
    }

    public void addressMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeAddressBookMenu(); 
            switch(choice)
            {
                case 1:
                getPerson();
                break;
                case 2:
                //create person
                createPerson();
                break;
                case 3:
                //delete person
                deletePerson();
                break;
                case 4:
                // update person
                updatePerson();
                break;
                case 5:
                // listAllPersons();
                listAllPerson();
                break;
                default:
                exit = true;
                break;
            }//end else

        }//end while
    }

    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** AddressBook Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");        
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    //the method reads id for a person and return
    // 
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Id:  ");
            try{
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }

    public long inputPersonsIDUpdate()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Id:  ");
            try{
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }

    public String inputName()
    {
        // makes an object keyboard to have input from the screen 

        boolean ok = false;        
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Name:  ");
        String name = keyboard.nextLine();
        while(!ok)
        {        

            if(name.matches("[a-zA-Z]+"))
            {
                ok = true;               
            }
            else
            {
                System.out.println("You must enter letters, from a-z or A-Z, - try again");
                name = keyboard.nextLine();
            }            
        }
        return name;
    }

    public int inputPostalCode()
    {
        // makes an object keyboard to have input from the screen

        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Postal Code:  ");
            try{
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
        //          Scanner keyboard = new Scanner(System.in);  
        //          System.out.println(" Write Postal Code:  ");
        //          String code = keyboard.nextLine();
        //          return code;
    }

    public String inputCity()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write City:  ");
        String city = keyboard.nextLine();
        while(!ok)
        {        

            if(city.matches("[a-zA-Z]+"))
            {
                ok = true;               
            }
            else
            {
                System.out.println("You must enter letters - try again");
                city = keyboard.nextLine();
            }            
        }
        return city;
    } 

    public int inputPhone()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Phone Number:  ");
            try{
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
        //          Scanner keyboard = new Scanner(System.in);  
        //          System.out.println(" Write Phone Number:  ");
        //          String phone = keyboard.nextLine();
        //          return phone;
    } 

    public String inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }

//     //??
//     public void showId(long id)
//     {
//         System.out.println("ID of the person: " + id);
//     }
//     //??
//     public void showName(String name)
//     { 
//         System.out.println("The name of the persone: " + name);
//     }
//     //??
//     public void showAddress(String address)
//     { 
//         System.out.println("The address of the person: " + address);
//     }

    public void getPerson()
    {//is to be changed when the address book controller is completed
        Scanner keyboard = new Scanner(System.in);
        long id = inputPersonsID();  
        if(abCtr.getPerson(id) != null)
        {
            abCtr.getPerson(id).printDetail();       
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("No Persons with the id "+id+"exist - try another");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();

    }   

    public void createPerson()
    {                
        Scanner keyboard = new Scanner(System.in);

        long id = (inputPersonsID());
        abCtr.addPerson(id, inputName(), inputAddress(), inputPostalCode(), inputCity() ,inputPhone());
        if(abCtr.getPerson(id) != null)
        {            
            System.out.println();
            System.out.println("New Person is created - se info above");        
            abCtr.getPerson(id).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("Something went wrong - contact an administrator or try again! ");
            System.out.println();
            System.out.println("Enter to continue...");
        }
        keyboard.nextLine();
    }

    public void updatePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        long id1 = inputPersonsID();
        long id2 = inputPersonsIDUpdate();
        if(abCtr.getPerson(id1) != null)
        {
            abCtr.updatePerson(id1,id2, inputName(), inputAddress(), inputPostalCode(), inputCity() ,inputPhone());
            System.out.println();
            System.out.println("Person is update - se info above");        
            abCtr.getPerson(id2).printDetail();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("No Persons with the id "+id1+"exist - try another ");
            System.out.println();
            System.out.println("Enter to continue...");       
        }
        keyboard.nextLine();

    }

    public void deletePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        long id = inputPersonsID();
        if(abCtr.getPerson(id) != null)
        {
            abCtr.getPerson(id).printDetail(); 
            System.out.println();
            System.out.println("The person with the id: "+id+" is now deleted");
            keyboard.nextLine();
            abCtr.deletePerson(id);
        }
        else
        {
            System.out.println();
            System.out.println("No Persons with the id "+id+"exist - try another ");
            System.out.println();
            System.out.println("Enter to continue...");
        }

    }

    public void listAllPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        if(abCtr.getSize() != 0)
        {
            abCtr.listAllPersons();
            System.out.println();
            System.out.println("Enter to continue...");
        }
        else
        {
            System.out.println();
            System.out.println("There is no Persons ");
            System.out.println();
            System.out.println("Enter to continue...");   
        }
        keyboard.nextLine();
    }
}

